package net.learn.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.query.SysRadioQuery;
import net.learn.system.query.SysVideoDetailQuery;
import net.learn.system.query.SysVideoQuery;
import net.learn.system.service.SysVideoDetailService;
import net.learn.system.service.SysVideoService;
import net.learn.system.vo.SysRadioVO;
import net.learn.system.vo.SysVideoDetailVO;
import net.learn.system.vo.SysVideoVO;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * 视频管理
 */
@RestController
@RequestMapping("sys/video")
@Tag(name="视频管理")
@AllArgsConstructor
public class SysVideoController {
    @Autowired
    SysVideoService videoService;

    @Autowired
    SysVideoDetailService videoDetailService;

    @GetMapping("getVideo")
    @Operation(summary = "分页获微课列表")
    public Result<PageResult<SysVideoVO>> getVideo(@Valid SysVideoQuery query){
        PageResult<SysVideoVO> page = videoService.qryVideoListPage(query);
        return Result.ok(page);
    }
    @PutMapping
    @Operation(summary = "修改")
    public Result<String> updateVideo(@RequestBody @Valid SysVideoVO vo){
        videoService.updateVideo(vo);

        return Result.ok();
    }
    @PostMapping("getVideoList")
    @Operation(summary = "获取微课列表")
    public Result<List<SysVideoVO>> getVideoList(@RequestBody @Valid SysVideoQuery query){
        List<SysVideoVO> list = videoService.qryVideoList(query);
        return Result.ok(list);
    }
    @PostMapping("save")
    @Operation(summary = "新增微课")
    public Result<String> addVideo(@RequestBody SysVideoVO vo){
        videoService.addVideo(vo);

        return Result.ok();
    }
    @PostMapping("deleteById")
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody @Valid SysVideoQuery query){
        SysVideoDetailQuery qryDetail=new SysVideoDetailQuery();
        qryDetail.setParentId(query.getId().toString());
        List<SysVideoDetailVO> detailList= videoDetailService.qryVideoDetailList(qryDetail);
        for (SysVideoDetailVO detailVo: detailList ) {
            List<Long> idlist=new ArrayList<>();
            idlist.add(detailVo.getId());
            videoDetailService.delete(idlist);
        }
        List<Long> idlist=new ArrayList<>();
        idlist.add(query.getId());
        videoService.delete(idlist);
        return Result.ok();
    }
}

